<template>
  <div>
    <h1 align="center">优惠券添加</h1>
    <table class="table table-bordered">
      <tr>
        <td align="center">类型券</td>
        <td>
          <select v-model="state.kid">
            <option value="">全部</option>
            <option :value="item.kid" v-for="item in kuu">{{ item.k_name }}</option>
          </select>
        </td>
      </tr>
      <tr>
        <td align="center">主题</td>
        <td>
          <input type="text" v-model="state.y_titile" />
        </td>
      </tr>
      <tr>
        <td align="center">颜色</td>
        <td>
          <input type="color" v-model="state.y_color" />
        </td>
      </tr>
      <tr>
        <td align="center">库存量</td>
        <td>
          <input type="text" v-model="state.y_number" />
        </td>
      </tr>
      <tr>
        <td align="center">限量</td>
        <td>
          <input type="text" v-model="state.y_num" />
        </td>
      </tr>
      <tr>
        <td align="center">使用条件</td>
        <td>
          <input type="text" v-model="state.y_price" />
        </td>
      </tr>
      <tr>
        <td align="center">减免金额</td>
        <td>
          <input type="text" v-model="state.y_jprice" />
        </td>
      </tr>
      <tr>
        <td align="center">折扣</td>
        <td>
          <input type="text" v-model="state.y_zprice" />
        </td>
      </tr>
      <tr>
        <td align="center">门店</td>
        <td>
          <select v-model="state.mid">
            <option value="">全部</option>
            <option :value="item.mid" v-for="item in fuu">{{ item.m_name }}</option>
          </select>
        </td>
      </tr>
      <tr>
        <td align="center">使用须知</td>
        <td>
          <textarea cols="30" rows="3" v-model="state.y_desc"></textarea>
        </td>
      </tr>
      <tr>
        <td align="center" colspan="2">
          <input type="button" value="新增" @click="add" />
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
const router = useRouter();
const route = useRoute();
let data: any = reactive({
  state: {
    y_titile: '',
    y_color: '',
    y_number: 0,
    y_num: 0,
    y_price: 0,
    y_jprice: 0,
    y_zprice: 0,
    mid: 0,
    kid: 0,
    y_sate: 1,
    y_desc: ''
  },
  fuu: [],
  kuu: []
});
let { state, fuu, kuu } = toRefs(data);
onMounted(() => {
  Getmmdian();
  Getmmyouhui();
});

const Getmmdian = () => {
  axios({
    url: '/api/JIuidan/QQQuantype',
    method: 'get'
  })
    .then((res) => {
      console.log(res);
      kuu.value = res.data.data;
    })
    .catch((err) => {
      console.log(err);
    });
};
const Getmmyouhui = () => {
  axios({
    url: '/api/JIuidan/m_MdiamModels',
    method: 'get'
  })
    .then((res) => {
      console.log(res);
      fuu.value = res.data.data;
    })
    .catch((err) => {
      console.log(err);
    });
};

const add = () => {
  let obj = {
    y_titile: state.value.y_titile,
    y_color: state.value.y_color,
    y_number: state.value.y_number,
    y_num: state.value.y_num,
    y_price: state.value.y_price,
    y_jprice: state.value.y_jprice,
    y_zprice: state.value.y_zprice,
    mid: state.value.mid,
    kid: state.value.kid,
    y_desc: state.value.y_desc
  };
  //验证
  if (state.value.y_titile=="") {
    alert("标题不能为空")
    return
  }
  axios({
    url: '/api/JIuidan/YaDD',
    method: 'post',
    data: obj
  })
    .then((res) => {
      console.log(res);
      if (res.data.data == 1) {
        alert('新增成功');
        router.push('/Rshow');
      }
    })
    .catch((err) => {
      console.log(err);
    });
};
</script>

<style scoped></style>
